package logic

import (
	"context"
	"rpc/model/mysql"

	"rpc/goods"
	"rpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodsListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodsListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodsListLogic {
	return &GoodsListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GoodsListLogic) GoodsList(in *goods.GoodsListRequest) (*goods.GoodsListResponse, error) {
	// todo: 商品列表

	g := mysql.GoodsZye{}

	goodsList, err := g.GoodsList(in.Keyword, int(in.Page), int(in.PageSize))
	if err != nil {
		logx.Error("列表查询失败")
		return nil, err
	}
	var list []*goods.GoodsList

	for _, v := range goodsList {
		list = append(list, &goods.GoodsList{
			GoodsTitle:  v.GoodsTitle,
			GoodsPrice:  float32(v.GoodsPrice),
			GoodsStock:  int64(v.GoodsStock),
			GoodsStatus: int64(v.GoodsStatus),
			GoodsAvatar: v.GoodsAvatar,
		})
	}

	return &goods.GoodsListResponse{List: list}, nil
}
